package com.ist.smartstation.dao.db.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.ist.smartstation.dao.PassengerDao;
import com.ist.smartstation.entity.Passenger;

public class PassengerSqliteDao implements PassengerDao {

	private static final String DATABASE_TABLE = "passengers";
	public static final String KEY_NAME = "name";
	public static final String KEY_DEST = "destination";
	public static final String KEY_NUM = "partynumber";
	public static final String KEY_ROWID = "_id";

	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private final Context mCtx;

	/**
	 * Constructor - takes the context and opens the database
	 * 
	 * @param ctx
	 *            the Context within which to work
	 */
	public PassengerSqliteDao(Context ctx) {
		this.mCtx = ctx;
		this.open();
	}

	/**
	 * Open the places database. If it cannot be opened, try to create a new
	 * instance of the database. If it cannot be created, throw an exception to
	 * signal the failure
	 * 
	 * @return this (self reference, allowing this to be chained in an
	 *         initialization call)
	 * @throws SQLException
	 *             if the database could be neither opened or created
	 */
	public PassengerSqliteDao open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	/**
	 * Create a new place using the name and description provided. If the place
	 * is successfully created return the new rowId for that place, otherwise
	 * return a -1 to indicate failure.
	 * 
	 * @param name
	 *            the name of the place
	 * @param description
	 *            the description of the place
	 * @return rowId or -1 if failed
	 */
	@Override
	public Passenger createPassenger(Passenger passenger) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, passenger.mName);
		initialValues.put(KEY_DEST, passenger.mDestination);
		initialValues.put(KEY_NUM, passenger.mPartyNumber);
		passenger.mId = mDb.insert(DATABASE_TABLE, null, initialValues);
		return passenger;
	}

	/**
	 * Delete the place with the given rowId
	 * 
	 * @param rowId
	 *            id of place to delete
	 * @return true if deleted, false otherwise
	 */
	public boolean deletePassenger(long rowId) {
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all places in the database
	 * 
	 * @return Cursor over all places
	 */
	public Cursor fetchAllPassengers() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME, KEY_NUM,
				KEY_DEST }, null, null, null, null, null);
	}

	/**
	 * Return a Cursor positioned at the place that matches the given rowId
	 * 
	 * @param rowId
	 *            id of place to retrieve
	 * @return Cursor positioned to matching place, if found
	 * @throws SQLException
	 *             if place could not be found/retrieved
	 */
	public Passenger fetchPassenger(long rowId) throws SQLException {
		Cursor cursor = null;
		try {
			cursor = mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID,
					KEY_NAME, KEY_DEST }, KEY_ROWID + "=" + rowId, null, null,
					null, null, null);
			if (cursor != null) {
				cursor.moveToFirst();
			}

			Passenger passenger = new Passenger();
			passenger.mName = cursor.getString(
					cursor.getColumnIndexOrThrow(KEY_NAME));
			passenger.mDestination = cursor.getString(
					cursor.getColumnIndexOrThrow(KEY_DEST));
			passenger.mPartyNumber = cursor.getInt(
					cursor.getColumnIndexOrThrow(KEY_NUM));
			return passenger;
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
	}

	/**
	 * Update the place using the details provided. The place to be updated is
	 * specified using the rowId, and it is altered to use the name and
	 * description values passed in
	 * 
	 * @param rowId
	 *            id of place to update
	 * @param name
	 *            value to set place name to
	 * @param description
	 *            value to set place description to
	 * @return true if the place was successfully updated, false otherwise
	 */
	@Override
	public boolean updatePassenger(Passenger passenger) {
		ContentValues args = new ContentValues();
		args.put(KEY_NAME, passenger.mName);
		args.put(KEY_DEST, passenger.mDestination);
		args.put(KEY_NUM, passenger.mPartyNumber);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" +passenger.mId,
				null) > 0;
	}

}
